<?php

namespace App\Http\Controllers\Backend;

use Mews\Captcha\Facades\Captcha;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Models\User;
use App\Models\Role;

class AdminController extends Controller{

    //加载主页
    public function index(Request $request){ 
        return view('index');
    }

    public function list(Request $request)
    {
        $role_id = $request->input('role_id','');
        $username = $request->input('username','');
        $limit = $request -> input('limit',10);
        $page = $request -> input('page',1);

        $where = [];
        if(!empty($role_id)){
            $where[] = ['user.role_id',$role_id];
        }

        if(!empty($username)){
            $where[] = ['user.username','like','%'.$username.'%'];
        }


        //用户信息
        $user_data = User::select([
                    'user.id','user.username','user.nickname','user.role_id','user.email','user.head_pic','user.sexy',
                    'user.mobile','user.status','user.created_at','user.description as desc','role.role_name'])
                    ->join('role','user.role_id','=','role.id')
                    ->where($where)
                    ->where('user.status','<',3)
                    ->orderBy('user.updated_at','desc')
                    ->paginate($limit)
                    ->toArray();

        $data = [];

        if($user_data){
            $data = [
                'code'=>200,
                'msg'=>'success',
                'data'=>$user_data
            ];
        }else{
            $data = [
                'code'=>100,
                'msg'=>'查询数据异常',
                'data'=>[]
            ];
        }

        return response()->json($data);

        //return response(User::all()->jsonSerialize(),Response::HTTP_OK);

    }

    //禁用启用设置
    public function setStatus(Request $request){
        $id = $request->input('id');
        $status = $request->input('status');
        $user = User::findOrFail($id);
        $user->status = $status;
        $res = $user->save();

        if($res){
            $data = [
                'code'=>200,
                'msg'=>'修改成功'
            ];
        }else{
            $data = [
                'code'=>100,
                'msg'=>'修改失败'
            ];
        }
        return response()->json($data);
    }



    //用户信息添加
    public function save(Request $request){

        //dd($request->input('nickname'));

        $username = $request->input('username');
        $nickname = $request->input('nickname');
        $password = $request->input('pwd');
        $sexy = $request->input('sexy');
        $head_pic = $request->input('head_pic');
        $role_id = $request->input('role_id');
        $mobile = $request->input('mobile');
        $email = $request->input('email');
        $desc = $request->input('desc');

        $now = date('Y-m-d H:i:s');

        //数据验证

        //入库操作
        $user = new User();
        $user->username = $username;
        $user->nickname = $nickname;
        $user->password = md5($password);
        $user->sexy = $sexy;
        $user->head_pic = $head_pic;
        $user->role_id = $role_id;
        $user->mobile = $mobile;
        $user->email = $email;
        $user->description = $desc;
        $user->updated_at = $now;
        $user->created_at = $now;

        $user->status = 1;
    
        $data = [];
        $res = $user->save();  //添加成功返回true

        if($res){
            $data = [
                'code'=>200,
                'msg'=>'添加成功'
            ];
        }else{
            $data = [
                'code'=>100,
                'msg'=>'添加失败'
            ];
        }
        return response()->json($data);
    }

    //用户信息修改
    public function update(Request $request){

        //dd($request->input('nickname'));

        $id = $request->input('id');
        $nickname = $request->input('nickname');

        $sexy = $request->input('sexy');
        $head_pic = $request->input('head_pic');
        $role_id = $request->input('role_id');
        $mobile = $request->input('mobile');
        $email = $request->input('email');
        $desc = $request->input('desc');

        $now = date('Y-m-d H:i:s');

        //数据验证

        //入库操作
        $user = User::findOrFail($id);
        $user->nickname = $nickname;
        $user->sexy = $sexy;
        $user->head_pic = $head_pic;
        $user->role_id = $role_id;
        $user->mobile = $mobile;
        $user->email = $email;
        $user->description = $desc;
        $user->updated_at = $now;  
    
        $data = [];
        $res = $user->save();  //添加成功返回true

        if($res){
            $data = [
                'code'=>200,
                'msg'=>'修改成功'
            ];
        }else{
            $data = [
                'code'=>100,
                'msg'=>'修改失败'
            ];
        }
        return response()->json($data);
    }


    //获取验证码
    public function captcha(){
        $data = [
            'code'=>200,
            'captcha'=>Captcha::create("flat",true),
            'msg'=>'ok'
        ];

        return response()->json($data);

    }

    //登陆验证
    public function login(Request $request){
        $username = $request->input('username');
        $passwd = $request->input('passwd');
        $captcha = $request->input('code','');
        $key = $request->input('code_key');

        if(empty($username) || empty($passwd)){
            return response()->json(['code'=>40011,'msg'=>'用户名密码不能为空']);
        }

        if(empty($captcha)){
            return response()->json(['code'=>40011,'msg'=>'验证码不能为空']);
        }

        if(!captcha_api_check($captcha,$key,'flat')){
            return response()->json(['code'=>40011,'msg'=>'验证码错误']);
          }

        $user = User::where('username',$username)->first();
        //dd($user);
        if(!$user){
            return response()->json(['code'=>40011,'msg'=>'用户账号不存在']);
        }

        if($user->password !== md5($passwd)){
            return response()->json(['code'=>40011,'msg'=>'用户账号或密码错误']); 
        }

        if($user->status ==2 ){
            return response()->json(['code'=>40011,'msg'=>'该账号已禁用']);
        }

        //加入session或redis
        //生成sessionid
        //$request->session()->regenerate();
        $request->session()->put('username', $user->username);
        $request->session()->put('nickname', $user->nickname);
        $request->session()->put('mobile', $user->mobile);
        $request->session()->put('sexy', $user->sexy);
        $request->session()->put('head_pic',$user->head_pic);
        $request->session()->put('role', $user->role);

        /*
        session([
            'username' => $user->username,
            'nickname' => $user->nickname,
            'mobile' => $user->mobile,
            'sexy' => $user->sexy,
            'head_pic' => $user->head_pic,
            'role'=>$user->role       
        ]);
        */

        return response()->json(['code'=>200,'msg'=>'登录成功']);
          
    }


}